//
//  COChatListTableViewCell.swift
//  CocoProject
//
//  Created by Ruthless on 2023/8/24.
//

import UIKit
let bgColor: UIColor = .rgba(17, 7, 41, 1)
class COChatListTableViewCell: UITableViewCell {
    var userId: String? {
        didSet {
            guard let userId = userId else { return }
            let formatter = DateFormatter()
            formatter.dateFormat = "HH:mm"
            if let date = UserDefaults.standard.object(forKey: "chatTime-\(userId)") as? Date {
                timeLabel.text = formatter.string(from: date)
            } else {
                timeLabel.text = formatter.string(from: Date())
            }
            
            if let data = UserDefaults.standard.object(forKey: "chat-\(userId)") as? Data {
                if let contents = try? NSKeyedUnarchiver.unarchivedObject(ofClass: NSArray.self, from: data) as? [String] {
                    contentLabel.text = contents.last ?? "hi"
                }
            }
            let avatar = UserDefaults.standard.string(forKey: "userAvatar---\(userId)") ?? ""
            avatarUrl = avatar
            self.avatar.kf.setImage(with: URL(string: avatar))
            nameLabel.text = UserDefaults.standard.string(forKey: "userName---\(userId)")
            userName = nameLabel.text ?? ""
            
        }
    }
    
    var avatarUrl = ""
    var userName = ""
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configUI() {
        backgroundColor = bgColor
        contentView.addSubview(avatar)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(lineView)
        
        avatar.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.width.height.equalTo(60)
            make.left.equalToSuperview().offset(20)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(15)
            make.top.equalTo(avatar.snp.top).offset(7)
            make.right.equalTo(timeLabel.snp.left).offset(-15)
        }
        
        timeLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-20)
            make.centerY.equalTo(nameLabel)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(avatar.snp.right).offset(15)
            make.bottom.equalTo(avatar.snp.bottom).offset(-9)
            make.right.equalToSuperview().offset(-75)
        }
        
        lineView.snp.makeConstraints { make in
            make.right.bottom.equalToSuperview().offset(0)
            make.left.equalTo(avatar.snp.right).offset(15)
            make.height.equalTo(1)
        }
        
    }
    private lazy var avatar: UIImageView = {
        let imgV = UIImageView()
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 30
        return imgV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(15)
        label.textColor = .white
        label.text = "1123"
        return label
    }()
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .white
        label.text = "jkldsalaljksdljalkjkljsadjsakdllkajajldksladksadslk"
        return label
    }()
    
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .white.withAlphaComponent(0.5)
        label.text = "11:20"
        return label
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = .white.withAlphaComponent(0.15)
        return view
    }()
}
